package com.bh.weapp.controller.common;

import com.bh.common.annotation.RepeatSubmit;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.extend.domain.JcOrderBarcode;
import com.bh.extend.service.IJcOrderBarcodeService;
import com.bh.order.convert.JcOrderConvert;
import com.bh.order.domain.JcOrder;
import com.bh.order.service.IJcOrderService;
import com.bh.order.vo.JcOrderCommonVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单Controller
 *
 * @author slx
 * @date 2023-04-22
 */
@Api(tags = {"COMMON公共订单查询API"})
@RestController
@RequestMapping("/api/common/order")
public class JcCommonOrderApiController extends BaseController {

    @Autowired
    private IJcOrderService jcOrderService;
    @Autowired
    private IJcOrderBarcodeService jcOrderBarcodeService;
    @Autowired
    private JcOrderConvert jcOrderConvert;

    /**
     * 查询订单列表
     */
    @ApiOperation("用户公共查询订单")
    @ApiImplicitParam(name = "code", value = "登录名称", dataType = "String", required = true, dataTypeClass = String.class)
    @RepeatSubmit
    @PostMapping("/getOrder/{code}")
    public AjaxResult getOrder(@PathVariable String code) {
        //需要转换为 JcOrderVo
        JcOrder order = jcOrderService.selectJcOrderByCode(code);
        if (order == null) {
            // 再去查询商品code
            JcOrderBarcode barcode = jcOrderBarcodeService.selectJcOrderBarcodeByProductCode(code);
            if (barcode != null) {
                order = jcOrderService.selectJcOrderById(barcode.getOrderId());
            }
        }
        JcOrderCommonVo orderCommonVo = jcOrderConvert.toCommonVo(order);
        return AjaxResult.success(orderCommonVo);
    }
}
